package com.pet_service.controller;

import com.pet_service.result.Result;
import com.pet_service.service.StaffScheduleService;
import com.pet_service.service.StaffService;
import com.pet_service.vo.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@Slf4j
@Tag(name = "服务人员管理接口")
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Resource
    private StaffService staffService;
    @Resource
    private StaffScheduleService staffScheduleService;

    //分页查询服务人员档案
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('Service:staff:view')")
    public Result<PageResultVO<StaffProfileVO>> getStaffList(@RequestBody PageBean pageBean) {
        log.info("分页查询服务人员档案，参数：{}", pageBean);

        // 1. 分页参数校验（用你的Result.fail()返回错误）
        if (pageBean.getPageNum() == null || pageBean.getPageSize() == null) {
            return Result.fail(500, "分页参数错误");
        }
        // 2. 计算start（从第几条数据开始查询）
        pageBean.setStart(pageBean.getPageSize() * (pageBean.getPageNum() - 1));

        // 3. 调用服务层查询（获取分页结果PageResultVO）
        PageResultVO<StaffProfileVO> pageResult = staffService.queryStaffProfiles(pageBean);

        // 4. 用你的Result.ok(T data)封装结果，返回Result<PageResultVO<StaffProfileVO>>
        return Result.ok(pageResult);
    }

    //根据ID查询单个服务人员档案
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('Service:staff:view')")
    public Result<StaffProfileVO> getStaffById(@PathVariable Integer id) {
        log.info("查询服务人员档案，ID：{}", id);
        StaffProfileVO staffProfile = staffService.queryStaffProfileById(id);

        // 用你的Result.fail()返回错误，ok(T data)返回成功数据
        if (staffProfile == null) {
            return Result.fail(500,"服务人员不存在或未分配服务人员角色");
        }
        return Result.ok(staffProfile);
    }


    //添加服务人员
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('Service:staff:add')")
    public Result addStaff(@RequestBody StaffFormDTO staffFormDTO) {
        log.info("添加服务人员，参数：{}", staffFormDTO);
        return staffService.addStaff(staffFormDTO);
    }

    //修改服务人员
    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('Service:staff:edit')")
    public Result updateStaff(@RequestBody StaffFormDTO staffFormDTO) {
        log.info("修改服务人员，参数：{}", staffFormDTO);
        return staffService.updateStaff(staffFormDTO);
    }

    //删除服务人员
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('Service:staff:delete')")
    public Result deleteStaff(@PathVariable Integer id) {
        log.info("删除服务人员，ID：{}", id);
        return staffService.deleteStaff(id);
    }

    //------------------------服务人员排班管理-----------------------
    //分页查询服务人员排班
    @PostMapping("/schedule/list")
    @PreAuthorize("hasAuthority('Service:staff:schedule:view')")
    public Result<PageResultVO<StaffScheduleVO>> getStaffScheduleList(@RequestBody PageBean pageBean) {
        log.info("分页查询服务人员排班，参数：{}", pageBean);

        // 1. 分页参数校验
        if (pageBean.getPageNum() == null || pageBean.getPageSize() == null) {
            return Result.fail(500, "分页参数错误");
        }
        // 2. 计算start
        pageBean.setStart(pageBean.getPageSize() * (pageBean.getPageNum() - 1));

        // 3. 调用服务层查询
        PageResultVO<StaffScheduleVO> pageResult = staffScheduleService.queryStaffSchedules(pageBean);

        return Result.ok(pageResult);
    }

    //根据ID查询单个服务人员排班
    @GetMapping("/schedule/{id}")
    @PreAuthorize("hasAuthority('Service:staff:schedule:view')")
    public Result<StaffScheduleVO> getStaffScheduleById(@PathVariable Integer id) {
        log.info("查询服务人员排班，ID：{}", id);
        StaffScheduleVO staffSchedule = staffScheduleService.queryStaffScheduleById(id);

        if (staffSchedule == null) {
            return Result.fail(500, "排班记录不存在");
        }
        return Result.ok(staffSchedule);
    }

    //添加服务人员排班
    @PostMapping("/schedule/add")
    @PreAuthorize("hasAuthority('Service:staff:schedule:add')")
    public Result addStaffSchedule(@RequestBody StaffScheduleVO staffScheduleVO) {
        log.info("添加服务人员排班，参数：{}", staffScheduleVO);
        return staffScheduleService.addStaffSchedule(staffScheduleVO);
    }

    //修改服务人员排班
    @PostMapping("/schedule/edit")
    @PreAuthorize("hasAuthority('Service:staff:schedule:edit')")
    public Result updateStaffSchedule(@RequestBody StaffScheduleVO staffScheduleVO) {
        log.info("修改服务人员排班，参数：{}", staffScheduleVO);
        return staffScheduleService.updateStaffSchedule(staffScheduleVO);
    }

    //删除服务人员排班
    @DeleteMapping("/schedule/delete/{id}")
    @PreAuthorize("hasAuthority('Service:staff:schedule:delete')")
    public Result deleteStaffSchedule(@PathVariable Integer id) {
        log.info("删除服务人员排班，ID：{}", id);
        return staffScheduleService.deleteStaffSchedule(id);
    }
}